package com.photographysharing.controller;


import com.photographysharing.entity.Photos;
import com.photographysharing.exception.ServiceException;
import com.photographysharing.response.MessageResponse;
import com.photographysharing.service.CategoriesService;
import com.photographysharing.service.MailService;
import com.photographysharing.util.RedisHelp;
import jakarta.annotation.Resource;
import jakarta.mail.Message;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.concurrent.ThreadLocalRandom;

@RestController
public class TestController {

    private RedisHelp redisHelp;
    private MailService emaiService;
    public TestController(RedisHelp redisHelp, MailService emaiService) {
        this.redisHelp = redisHelp;
        this.emaiService = emaiService;
    }





    @Resource
    private CategoriesService categoriesService;

    @GetMapping
    @RequestMapping("/send")
    public MessageResponse send(){
        try{
            var b = redisHelp.getString("123@qq.com");
            String code = creatCode(4);
            redisHelp.setString("123@qq.com",code.toLowerCase(),15);
            var a = redisHelp.getString("123@qq.com");
            emaiService.sendCodeMailMessage("zzh164@qq.com",code);
            redisHelp.removeKey("123@qq.com");
            return null;

        }catch (Exception e){
            throw new ServiceException("出错啦");
        }
    }
    private  String creatCode(int n) {

        String code = "";
        Random r = new Random();
        //2.在方法内部使用for循环生成指定位数的随机字符，并连接起来
        for (int i = 0; i <= n; i++) {
            //生成一个随机字符：大写 ，小写 ，数字（0  1  2）
            int type = r.nextInt(3);
            switch (type) {
                case 0:
                    char ch = (char) (r.nextInt(26) + 65);
                    code += ch;
                    break;
                case 1:
                    char ch1 = (char) (r.nextInt(26) + 97);
                    code += ch1;
                    break;
                case 2:
                    code +=  r.nextInt(10);
                    break;
            }

        }
        return code;
    }


    @GetMapping
    @RequestMapping("/test")
    public MessageResponse test(){


        try{



//            QueryWrapper<Categories> QR=new QueryWrapper();
//            Categories  categories = categoriesService.getOne(QR.eq("name","无"));
//            var a = categories;
            redisHelp.setString("123","234");
            var a = redisHelp.getString("123");
            return new MessageResponse("ok");
        }catch (Exception e){
            throw new ServiceException("出错啦");
        }
    }
}
